package session.stateless;

import entity.Customer;
import entity.LineItem;
import entity.Product;
import entity.PurchaseOrder;
import entity.SaleLead;
import entity.SaleQuotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PurchaseOrderBean implements PurchaseOrderBeanLocal {

    @PersistenceContext()
    EntityManager em;
    PurchaseOrder purchaseOrder;
    
    public LineItem createLineItem(Product product, Integer quantity) 
    {
            //LineItem has attributes id, quantity and product
            LineItem lineItem = new LineItem();
            
            lineItem.setLineItemQuantity(quantity);
            lineItem.setProduct(product);

            em.persist(lineItem);
            return lineItem;
    }
    
    public Long createPOWithSQ(Long customerId, Long saleQuotationId, List<LineItem> lineItems) 
    {
        Long id = null;
        Customer customer = em.find(Customer.class, customerId);
        SaleQuotation saleQuotation = em.find(SaleQuotation.class, saleQuotationId);        
        
        List<LineItem> temp_lineItems = new ArrayList<LineItem>();
        for(LineItem o: lineItems) 
        {
            LineItem temp_lineItem = o;
            em.persist(temp_lineItem);
            temp_lineItems.add(temp_lineItem);
        }
                
        id = convertToPurchaseOrder(customerId, saleQuotationId, temp_lineItems);
        return id;
    }
    
    public Long createPOWithoutSQ(Long customerId, List<LineItem> lineItems) 
    {
        Long id = null;            
        Customer customer = em.find(Customer.class, customerId);
        
        List<LineItem> temp_lineItems = new ArrayList<LineItem>();
        for(LineItem o: lineItems) 
        {
            LineItem temp_lineItem = o;
            em.persist(temp_lineItem);
            temp_lineItems.add(temp_lineItem);
        }
        
        if (customer == null)
        {
            SaleLead saleLead = em.find(SaleLead.class, customerId);
            if (saleLead == null)
            {
                System.out.println("Sale Lead does not exist.");
            }
            else 
            {
                //convert sale lead to customer
            }
        }
        
        id = createNewPurchaseOrder(customerId, temp_lineItems);

        return id;
    }
        
    public Long createNewPurchaseOrder(Long customerId, List<LineItem> lineItems)
    {
        purchaseOrder = new PurchaseOrder();
        
        Customer customer = em.find(Customer.class, customerId);
        
        purchaseOrder.setCustomer(customer);
        purchaseOrder.setLineItems(lineItems);
            //add requestedDeliveryDate, final price, total
        customer.getPurchaseOrders().add(purchaseOrder);
        em.persist(purchaseOrder);
        
        return purchaseOrder.getId();
    }
    
    public Long convertToPurchaseOrder(Long customerId, Long saleQuotationId, List<LineItem> lineItems)
    {
        purchaseOrder = new PurchaseOrder();
        
        Customer customer = em.find(Customer.class, customerId);
        SaleQuotation saleQuotation = em.find(SaleQuotation.class, saleQuotationId);
        
        purchaseOrder.setCustomer(customer);
        purchaseOrder.setSaleQuotation(saleQuotation);
        purchaseOrder.setLineItems(lineItems);
        customer.getPurchaseOrders().add(purchaseOrder);
        em.persist(purchaseOrder);
      
        return purchaseOrder.getId();
    }
 
    public void updatePurchaseOrderId(Long purchaseOrderId) {
        purchaseOrder = em.find(PurchaseOrder.class, purchaseOrderId);
        purchaseOrder.setId(purchaseOrderId);
    }
    
    public List<Product> getProduct() {
        Query q = em.createQuery("SELECT c FROM Product c");
        
        return q.getResultList();
    }
        
        public List<PurchaseOrder> getPurchaseOrders() {
            Query q = em.createQuery("SELECT c from PurchaseOrder c");
     
            return q.getResultList();
    }
}
